<?php
/**
 * Created by : PhpStorm
 * User: Shane
 * Date: 2023-03-24
 * Time: 17:24
 */

namespace app\admin\controller;

use app\admin\model\Nav;
use app\AdminBaseController;
use app\admin\model\Role as roleModel;

class Role extends AdminBaseController
{
    /**
     * 角色列表
     */
    public function index(){
        $adminModel = new roleModel();
        $where[]=["is_del","=","0"];
        $where[]=["id",">","1"];
        $where=array_merge($adminModel->search(),$where);
        $res = $adminModel->getList("role",$this->page, $this->size, $where);
        $assignData=[
            "res"        => $res['data'],
            "search"     => $this->request->get(),
            "query"      => $this->query,
            "count"      => $res['count'],
            "pageTotal"  => $this->size,
            "curr"       => $this->page,
        ];
        return view('index',$assignData);
    }
    /**
     * 添加角色
     */
    public function add()
    {
        if($this->request->post()){
            $roleModel = new roleModel();
            $data=$roleModel->validate();
            $data["createdate"]=time();
            if(isset($data["role"]))$data["role"]=implode(",",$data["role"]);
            $this->dbSave("role",$data);
        }
        $nav = new Nav();
        return view('add',["navArr"=>$nav->getNavList()]);
    }
    /**
     * 修改角色
     */
    public function update(){
        $adminModel = new roleModel();
        if($this->request->post()){
            $data=$adminModel->validate();
            $data["createdate"]=time();
            if(isset($data["role"]))$data["role"]=implode(",",$data["role"]);
            $this->dbUpdate("role",$data,["id"=>$data["id"]]);
        }
        $nav = new Nav();
        $navArr = $nav->getNavList();// 获取菜单列表
        $roleArr=$adminModel->where(["id"=>$this->data["id"]])->find();
        if($roleArr) $roleArr["role"]=explode(",",$roleArr["role"]);
        return view('update',["navArr"=>$navArr,"roleArr"=>$roleArr]);
    }
    /**
     * 更改状态 启用禁用
     */
    public function status(){
        $roleModel = new roleModel();
        $data=$roleModel->validate();
        $this->dbUpdate("role",$data,["id"=>$data["id"]]);
    }
    /**
     * 批量删除 单个删除
     */
    public function delete(){
        $roleModel = new roleModel();
        $data=$roleModel->validate();
        $where[] = ['id', 'in', explode(',', $data['id'])];
        $this->dbDelete("role", $where);
    }
}